require("../util.js");
require("../component/components.js");
// require("../component/purchaseHeader.js");
// require("../component/footer.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#discount",
    data:{
        pageNumber:1,
        discounts:{},
        discountsList:{},
        discountsList1:[],
        discountsList2:[],
        hours:"",//时
        minutes:"",//分
        seconds:"",//秒
        endSecond:"",
        startsecond:""

    },
    mounted:function(){
        this.getDiscountsList();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //乐购折扣列表
        getDiscountsList:function(){
            var that=this;
            this.ajax({url:"/purchase/getDiscountsList",data:{pageNumber:that.pageNumber},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.discounts=b.json.data;
                    that.discountsList=that.discounts.records;
                    that.discountsList1=that.discountsList.list;
                    if(that.discountsList.pageNumber>1){
                        if(that.discountsList.list.length>0) {
                            that.discountsList2 = that.discountsList2.concat(b.json.data.list);
                        }
                    }else{
                        that.discountsList2= that.discountsList1;
                    }
                    console.log(that.discountsList1)
                    // console.log(that.discountsList2);
                    that.endDate=that.discounts.endDate;
                    that.endSecond=parseInt(that.endDate)*60*60;
                    that.compareDate();
                    vm.loadFlag=false;
                    lazyload();
                }else{
                    that.discounts={};
                    that.discountsList={};
                    that.discountsList1=[];
                    that.discountsList2=[];
                    vm.$refs.prompt.toast("获取折扣列表失败！")
                }
            });
        },
        compareDate:function(){
            a=setInterval(this.leftTimer,1000);
            //this.leftTimer();
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            var date = new Date();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            this.startsecond=(hour*60+minute)*60+second;
            if(that.endSecond>that.startsecond){
                that.hours=Math.floor((that.endSecond-that.startsecond)/60/60);
                that.minutes=Math.floor((that.endSecond-that.startsecond)/60%60);
                that.seconds=Math.floor((that.endSecond-that.startsecond)%60);
                that.hours=checkTime(that.hours);
                that.minutes=checkTime(that.minutes);
                that.seconds=checkTime(that.seconds);
            }else{
                clearInterval(a);
            }
        },
        getNext:function(item){
            console.log(item);
            location.href="/purchase/goodsDetails?type=2&productId="+item.productId;
        }
    }
});
var a;
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
var loadFlag;
window.onscroll=function(){
    // if(getDocumentTop()>739){
    //     vm.isNav=2;
    // }else{
    //     vm.isNav=1;
    // }
    loadFlag = vm.discountsList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.data.pageNumber++;
            vm.getDiscountsList();
            lazyload();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}